<template>
  <div>
    <el-input
      style="width: 200px; float: left; margin-left: 20px"
      size="small"
      placeholder="请输入登录名称"
      v-model="username"
    />
    <el-input
      style="width: 200px; float: left; margin-left: 20px"
      size="small"
      placeholder="请输入所属学院"
      v-model="cname"
    />
    <el-button
      style="float: left; margin-left: 15px; margin-bottom: 10px"
      icon="el-icon-search"
      size="small"
      type="primary"
      @click="initUserOnline"
      >搜索</el-button
    >
    <el-button
      style="float: left; margin-left: 15px; margin-bottom: 10px"
      icon="el-icon-refresh"
      size="small"
      @click="clearInput"
      >重置</el-button
    >
    <el-table
      ref="multipleTable"
      :data="tableData"
      tooltip-effect="dark"
      style="width: 100%; margin-top: 54px"
      :cell-style="rowClass"
      :header-cell-style="headClass"
    >
      <el-table-column prop="id" label="ID" width="100"> </el-table-column>
      <el-table-column prop="userName" label="登录名称" width="100">
      </el-table-column>
      <el-table-column prop="cname" label="所属学院" width="200">
      </el-table-column>
      <el-table-column prop="ip" label="IP" width="100"> </el-table-column>
      <el-table-column prop="loginLocation" label="登录地点" width="200">
      </el-table-column>
      <el-table-column prop="browser" label="浏览器" width="100">
      </el-table-column>
      <el-table-column prop="os" label="操作系统" width="200">
      </el-table-column>
      <el-table-column prop="loginTime" label="登录时间" width="200">
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.userName != 'admin'"
            size="mini"
            type="danger"
            @click="kickOut(scope.row)"
            >强退</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      style="float: right; margin-top: 10px"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      background
      :current-page="currentPage"
      :page-sizes="[1, 8]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [],
      username: "",
      cname: "",
      pageSize: 1,
      currentPage: 1,
      total: 0,
    };
  },
  created() {
    this.initUserOnline();
  },
  methods: {
    handleCurrentChange(val) {
      this.currentPage = val;
      this.initUserOnline();
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.initUserOnline();
    },
    clearInput() {
      location.reload();
    },
    rowClass() {
      return "text-align: center;";
    },
    headClass() {
      return "text-align: center;background:#f8f8f9;";
    },
    initUserOnline() {
      this.$get(
        "online/?username=" + this.username + "&cname=" + this.cname
      ).then((res) => {
        if (res.code == 200) {
          this.tableData = res.data;
          this.total = res.data.length;
        }
      });
    },
    kickOut(row) {
      this.$confirm(
        '此操作将强退"' + row.userName + '"用户, 是否继续?',
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).then(() => {
        this.$delete("online/" + row.id).then((res) => {
          if (res.code == 200) {
            this.initUserOnline();
          }
        });
      });
    },
  },
};
</script>

<style>
</style>